<?php

namespace app\controller;

use app\BaseController;
use app\Request;
use app\model\Orders as OrdersModel;
use app\model\Recyclables as RecyclablesModel;

class Orders extends BaseController
{
    /*
     * 获取我的交易数量
     * */
    function getMyPayCount(Request $request)
    {
        $userId = $request->userId;
        //获取我发布的数量
        $publishCount =  RecyclablesModel::where(['user_id' => $userId])
            ->where(['recyclables_statu'=>'1'])
            ->count();
        //获取我卖出的数量
        $sellCount = OrdersModel::where(['publish_id' => $userId])->count();
        //获取我买到的数量
        $buyCount = OrdersModel::where(['reclaim_id' => $userId])->count();

        return json(['code'=>200,'publishCount'=>$publishCount,'sellCount'=>$sellCount,'buyCount'=>$buyCount]);
    }

    /*
     * 获取我发布的回收物
     * */
    function getMyPublish(Request $request)
    {
        $userId = $request->userId;
        $pageSize = $this->request->param('pageSize', 15); // 默认每页15条
        $currentPage = $this->request->param('currentPage', 1);// 默认第1页
        $myPublish = RecyclablesModel::where(['user_id' => $userId])
            ->where(['recyclables_statu'=>'1'])
            ->paginate([
            'page' => $currentPage,
            'list_rows' => $pageSize,
        ]);
        //没有发布的回收物
        if ($myPublish->total()==0) {
            return json(['code' => 559, 'msg' => '赶快去发布吧']);
        }
        return json(['code'=>200,'myPublish'=>$myPublish]);
    }

    /*
     * 删除我发布的回收物
     * */
    function delMyPublish(){
        //获取要删除的发布的回收物id
        $recyclablesId = $this->request->param('recyclablesId');

        if (empty($recyclablesId)) {
            return json(['code' => 555, 'msg' => '参数错误']);
        }
        //查找回收物
        $recyclables = RecyclablesModel::find($recyclablesId);
        //不存在
        if (empty($recyclables)) {
            return json(['code' => 558, 'msg' => '回收物不存在']);
        }
        //修改回收物的状态为删除
        $recyclables->recyclables_statu = -1;
        $delResults =$recyclables->save();

        if (!$delResults) {
            //数据库修改失败
            return json(['code' => 557, 'msg' => '删除失败']);
        }
        return json(['code'=>200,'msg' => '删除成功']);

    }

    /*
     * 编辑我发布的回收物
     * */
    function editMyPublish(Request $request)
    {
        //获取回收物的id
        $id = $this->request->param('recyclablesId');
        //判断有没有传入id
        if (empty($id)) {
            return json(['code' => 555, 'msg' => '参数错误']);
        }
        $Recyclables = RecyclablesModel::find($id);
        if (empty($Recyclables)) {
            return json(['code' => 560, 'msg' => '回收物不存在']);
        }
        return json(['code'=>200,'Recyclables'=>$Recyclables]);
    }

    /*
     * 获取我卖出的
     * */
    function getMySell(Request $request)
    {
        //获取用户id
        $userId = $request->userId;
        $pageSize = $this->request->param('pageSize', 15); // 默认每页15条
        $currentPage = $this->request->param('currentPage', 1);// 默认第1页

        //模型关联查询
        $mySell = OrdersModel::with(['recyclables'=>function ($query) {
            //指定回收物信息的字段
            $query->field('id,title,image,price');
        },'buyer' => function ($query) {
            //指定买家的字段
            $query->field('id,nick,avatar');
        }])
            ->where(['publish_id' => $userId])//卖家的id查询订单
            ->paginate([//分页查询
            'page' => $currentPage,
            'list_rows' => $pageSize,
        ]);
        if ($mySell->total()==0) {
            return json(['code' => 560, 'msg' => '快去发布看看呢']);
        }
        return json(['code'=>200,'mySell'=>$mySell]);
    }
    /*
     * 获取我买到的
     * */
    function getMyBuy(Request $request)
    {
        //获取用户id
        $userId = $request->userId;
        $pageSize = $this->request->param('pageSize', 15); // 默认每页15条
        $currentPage = $this->request->param('currentPage', 1);// 默认第1页

        //关联模型查询并分页
        $myBuy = OrdersModel::with(['recyclables'=>function ($query) {
            //指定回收物信息的字段
            $query->field('id,title,image,price');
        },'seller' => function ($query) {
            //指定卖家的字段
            $query->field('id,nick,avatar');
        }])
            ->where(['reclaim_id' => $userId])//买家的id查询订单
            ->paginate([
                'page' => $currentPage,
                'list_rows' => $pageSize,
            ]);
        if ($myBuy->total()==0) {
            return json(['code' => 560, 'msg' => '快去交易市场看看吧']);
        }
        return json(['code'=>200,'myBuy'=>$myBuy]);
    }


}